package br.com.ps.bombertournament.game.scene;

import br.com.ps.bombertournament.SceneManager;
import br.com.ps.bombertournament.game.Renderer;
import br.com.ps.bombertournament.game.Scene;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public abstract class BaseScene implements Scene {

	private Renderer renderer;
	private OrthographicCamera camera;
	private Music music;
	private SceneManager sceneManager;
	
	public BaseScene() {
		renderer = new Renderer();
		renderer.setScene(this);
	}

	public void create(SceneManager sceneManager) {
		this.sceneManager = sceneManager;
	}

	public void resume() {
		renderer.resume();
	}

	public void resize(int width, int height) {
		renderer.resize(width, height);
	}

	public void pause() {
		renderer.pause();
		
		if (music != null) {
			music.pause();
		}
	}

	public void dispose() {
		renderer.dispose();
		
		if (music != null) {
			music.dispose();
		}
	}
	
	public void removed() {
		pause();
	}
	
	@Override
	public void update(float deltaTime) {
		renderer.update(deltaTime);
	}

	@Override
	public void render(SpriteBatch spriteBatch) {
		if (camera != null) {
			camera.update();
			camera.apply(Gdx.graphics.getGL10());
			spriteBatch.setProjectionMatrix(camera.combined);
		}
		
		renderer.render(spriteBatch);
	}

	public Renderer getRenderer() {
		return renderer;
	}

	public OrthographicCamera getCamera() {
		return camera;
	}

	public void setCamera(OrthographicCamera camera) {
		this.camera = camera;
	}
	
	/**
	 * Return the current music of the scene
	 * 
	 * @return Music of the scene, can be null
	 */
	public Music getMusic() {
		return music;
	}

	/**
	 * Set the new music to the scene
	 * @param music New value to the music's scene
	 */
	public void setMusic(Music music) {
		this.music = music;
	}

	public SceneManager getSceneManager() {
		return sceneManager;
	}
}